<template>
  <div class="mod-config">
    <div class="z-querybar">
      <el-date-picker v-model="dataForm.daterange" style="width: 280px;" align="center" :clearable="false"
        type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
      </el-date-picker>
      <el-select filterable clearable v-model="dataForm.containerId" placeholder="请选择污水池" style="width: 210px;">
        <el-option v-for="container in containerList" :key="container.id" :label="container.name" :value="container.id"></el-option>
      </el-select>
      <el-select clearable v-model="dataForm.status" placeholder="请选择状态" style="width: 130px;">
        <el-option v-for="status in statusList" :key="status.value" :label="status.label" :value="status.value"></el-option>
      </el-select>
      <z-search-btn @click="currentChangeHandle(1)"></z-search-btn>
    </div>
    <div class="z-pagecontent">
      <el-table :data="dataList" border stripe v-loading="dataListLoading" style="width: 100%;" :height="maxHeight">
        <el-table-column type="index" header-align="center" label="序号" align="center" width="50">
        </el-table-column>
        <el-table-column prop="name" header-align="center" label="预警标题" align="center" min-width="250" show-overflow-tooltip>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="污水池" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ containerMap[scope.row.containerId + ''] && containerMap[scope.row.containerId + ''].name }}
          </template>
        </el-table-column>
        <el-table-column prop="alarmLiquidValue" header-align="center" align="center" label="预警液位" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.alarmLiquidValue && scope.row.alarmLiquidValue.toFixed(3) }}</template>
        </el-table-column>
        <el-table-column prop="alarmTime" header-align="center" align="center" label="预警时间" min-width="160" show-overflow-tooltip>
        </el-table-column>
        <!-- 1:处理中 2:处理完成 -->
        <el-table-column prop="status" header-align="center" align="center" label="处理状态" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">{{ statusMap[scope.row.status] }}</template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import { formatDate } from '@/utils/timeUtils'
import AddOrUpdate from './signcontaineralarm-add-or-update'
export default {
  components: { AddOrUpdate },
  data () {
    const now = new Date()
    const pre = new Date(now.getTime() - 3600000 * 24 * 7)
    return {
      maxHeight: document.documentElement.clientHeight - 260,
      statusList: [
        // { value: '0', label: '待处理' },
        { value: '1', label: '正在处理' },
        { value: '2', label: '已处理' }
      ],
      statusMap: {
        // '0': '待处理',
        '1': '正在处理',
        '2': '已处理'
      },
      containerList: [],
      containerMap: {},
      dataForm: {
        daterange: [pre, now],
        dispatchType: '',
        containerId: '',
        status: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted () {
    this.getContainerList()
    this.getDataList()
  },
  methods: {
    getContainerList () {
      this.$http({
        url: this.$http.adornUrl('/sign/signcontainer/realList'),
        method: 'post',
        data: this.$http.adornData({
          page: 1,
          limit: -1
        })
      }).then(({ data }) => {
        const containerMap = {}
        if (data && data.list) {
          this.containerList = data.list
          this.containerList.forEach(element => {
            containerMap[element.id + ''] = element
          })
        } else {
          this.containerList = []
        }
        this.containerMap = containerMap
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sign/signcontaineralarm/list'),
        method: 'post',
        data: this.$http.adornData({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'status': this.dataForm.status,
          'containerId': this.dataForm.containerId,
          'startTime': formatDate(this.dataForm.daterange[0], 'yyyy-MM-dd 00:00:00'),
          'endTime': formatDate(this.dataForm.daterange[1], 'yyyy-MM-dd 23:59:59'),
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>
